package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    //套餐列表
    @Override
    public Page<Setmeal> findPage(Integer page, Integer pageSize, String name) {
        //构建分页
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        //构建条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(name),Setmeal::getName,name);
        setmealPage = setmealMapper.selectPage(setmealPage, qw);
        //套餐内包含菜品还需要分类
        // 套餐分类
        List<Setmeal> setmeals = setmealPage.getRecords();
        for (Setmeal setmeal : setmeals) {
            Category category = categoryMapper.selectById(setmeal.getCategoryId());
            setmeal.setCategoryName(category.getName());

            //根据 表setmeal 的id  查询表setmeal—dish
//            LambdaQueryWrapper<SetmealDish> qw2 = new LambdaQueryWrapper<>();
//             qw2.eq(SetmealDish::getSetmealId, setmeal.getId());
//            List<SetmealDish> setmealDishes = setmealDishMapper.selectList(qw2);
//            setmeal.setSetmealDishes(setmealDishes);
        }
        return setmealPage;
    }

    /**
     * 回显套餐内容
     * @param id
     * @return
     */
    @Override
    public Setmeal findOne(Long id) {

        Setmeal setmeal = setmealMapper.selectById(id);
        // 查dish 集合
        //用setmeal id
        Long setmealId = setmeal.getId();
        //构建条件
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(setmealId!= null,SetmealDish::getSetmealId,setmealId);
        // 查！
        List<SetmealDish> dishes = setmealDishMapper.selectList(qw);
        setmeal.setSetmealDishes(dishes);
        return setmeal;
    }

    /**
     * 新增套餐
     * @param setmeal
     */
    @Override
    public void save(Setmeal setmeal) {
        setmealMapper.insert(setmeal);
        Long setmealId = setmeal.getId();
        //要把dish集合存进setmeal——dish表
        List<SetmealDish> dishes = setmeal.getSetmealDishes();
        for (SetmealDish dish : dishes) {
            dish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(dish);
        }
    }

    /**
     * 批量启停售
     * @param status
     * @param ids
     */
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        //构建条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId,ids);
        // 把状态存进去 更新进去
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmealMapper.update(setmeal,qw);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
        //构建条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(ids != null,Setmeal::getId,ids);
        //判断是否在停售
        qw.eq(Setmeal::getStatus,1);
        Integer count = setmealMapper.selectCount(qw);
        if (count>0){
            throw new CustomException("套餐中有启售状态，请先停售再删除~~~");
        }
        //删除套餐
        setmealMapper.deleteBatchIds(ids);
        //删除 setmeal-dish 内容
        LambdaQueryWrapper<SetmealDish> qw2 = new LambdaQueryWrapper<>();
        qw2.in(SetmealDish::getSetmealId,ids);
        setmealDishMapper.delete(qw2);
    }

    /**
     * 根据菜品分类id查询套餐列表
     * @param categoryId
     * @param status
     * @return
     */
    @Override
    public List<Setmeal> findByCategoryId(Long categoryId, Integer status) {
        // 构建条件  select * from setmeal where category_id =?
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId);
        qw.eq(Setmeal::getStatus,status);
        List<Setmeal> setmealList = setmealMapper.selectList(qw);
        return setmealList;
    }

    /**
     * 套餐id查内菜品list
     * @param setmealId
     * @return
     */
    @Override
    public List<Dish> getDishes(Long setmealId) {
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,setmealId);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(qw);
        List<Dish> dishes = new ArrayList<>();
        for (SetmealDish setmealDish : setmealDishes) { // update dish set ... = ... where id = dishId;
            Dish dish = dishMapper.selectById(setmealDish.getDishId());
            dish.setCopies(setmealDish.getCopies());
            dishes.add(dish);
        }
        return dishes;
    }
}
